package by.vkhalko.asamo.dataaccess.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import by.vkhalko.asamo.dataaccess.CustomerDao;
import by.vkhalko.asamo.datamodel.Company;
import by.vkhalko.asamo.datamodel.Customer;
import by.vkhalko.asamo.datamodel.Customer_;
import by.vkhalko.asamo.datamodel.Person;

@Repository
public class CustomerDaoImpl extends AbstractDaoImpl<Long, Customer> implements
		CustomerDao {

	protected CustomerDaoImpl() {
		super(Customer.class);

	}

	@Override
	public List<Customer> getAllByPerson(Person person) {
		return getAllByFieldRestriction(Customer_.person, person);
	}

	@Override
	public List<Customer> getAllByCompany(Company company) {
		return getAllByFieldRestriction(Customer_.company, company);
	}

	@Override
	public List<Customer> getAllCustomers() {
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<Customer> criteria = cBuilder.createQuery(Customer.class);
		Root<Customer> root = criteria.from(Customer.class);

		criteria.select(root);

		TypedQuery<Customer> query = getEm().createQuery(criteria);
		List<Customer> results = query.getResultList();
		return results;
	}

}
